package cn.itsource.order.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.query.BaseQuery;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.GetUserInfoUtil;
import cn.itsource.order.domain.OrderAddress;
import cn.itsource.order.domain.OrderAdopt;
import cn.itsource.order.domain.UserAddress;
import cn.itsource.order.dto.PayDto;
import cn.itsource.order.mapper.OrderAddressMapper;
import cn.itsource.order.mapper.OrderAdoptMapper;
import cn.itsource.order.mapper.UserAddressMapper;
import cn.itsource.order.service.IOrderAdoptService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.mapper.PetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Optional;


@Service
@Transactional
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt, BaseQuery> implements IOrderAdoptService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private OrderAdoptMapper orderAdoptMapper;

    @Autowired
    private AlipayService alipayService;

    @Override
    public String submitOrder(PayDto payDto) {

        //1.根据petId 查询宠物数据 (价格,shopId)
        Pet pet = petMapper.selectByPrimaryKey(payDto.getPetId());

        Optional.ofNullable(pet).orElseThrow(() -> new BusinessException("宠物信息不存在"));

        //2.通过addressId拿到地址对象 UserAddress  保存OrderAddress  返回的id存到订单里面的外键
        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(payDto.getAddressId());

        Optional.ofNullable(userAddress).orElseThrow(() -> new BusinessException("地址信息不存在"));

        //2.1拷贝属性
        OrderAddress orderAddress = BeanUtil.copyProperties(userAddress, OrderAddress.class);
        orderAddress.setId(null);
        orderAddress.setCreateTime(new Date());
        orderAddressMapper.insertSelective(orderAddress);

        //3.订单入库
        OrderAdopt orderAdopt = new OrderAdopt();
        String s = "购宠物订单:" + pet.getName() + ",金额:" + pet.getSaleprice();
        orderAdopt.setDigest(s);
        orderAdopt.setState(0);
        orderAdopt.setPrice(BigDecimal.valueOf(pet.getSaleprice()));
        String orderSn = IdUtil.getSnowflakeNextId() + "";
        orderAdopt.setOrderSn(orderSn); //雪花id
        orderAdopt.setLastConfirmTime(new Date());
        orderAdopt.setPetId(pet.getId());
        orderAdopt.setUserId(GetUserInfoUtil.getUserId());
        orderAdopt.setShopId(pet.getShopId());
        orderAdopt.setAddressId(orderAddress.getId());

        orderAdoptMapper.insertSelective(orderAdopt);

        //4.返回支付的html代码给前端
        return alipayService.createPayUrl(pet.getSaleprice(),s, orderSn);

    }
}
